package com.puyuba.puyuba.view.fragment;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import okhttp3.RequestBody;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.puyuba.puyuba.R;
import com.puyuba.puyuba.SkinApplication;
import com.puyuba.puyuba.view.dialog.ContactAdminDialog;
import com.pyb.java.basemodule.BaseFragment;
import com.puyuba.puyuba.bean.DownCardBean;
import com.puyuba.puyuba.bean.FindListBean;
import com.pyb.java.cache.CacheUserInfo;
import com.pyb.java.cache.SPHelper;
import com.pyb.java.cache.bean.UserInfoBean;
import com.puyuba.puyuba.network.BaseApis;
import com.pyb.java.commonutil.eventbus.EventBusKeys;
import com.pyb.java.commonutil.eventbus.EventBusUtils;
import com.pyb.java.network.RequestBodyUtil;
import com.pyb.java.network.ResponseEntity;
import com.pyb.java.network.RetrofitManger;
import com.pyb.java.network.observers.ResponseObserve;

import com.pyb.java.network.scheduler.RxSchedulers;
import com.puyuba.puyuba.network.UserApi;
import com.pyb.java.commonutil.utils.NetUtil;
import com.pyb.java.commonutil.ToastHelper;

import com.puyuba.puyuba.view.activity.MainWebActivity;
import com.puyuba.puyuba.view.adapter.FindTabAdatper;
import com.scwang.smart.refresh.header.ClassicsHeader;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;

import java.util.HashMap;
import java.util.List;

public class FindTabListFragment extends BaseFragment {
    private SmartRefreshLayout mSmartRefreshLayout;
    private RecyclerView mRecyclerView;

    private FindTabAdatper mAdapter;
    private int type;
    private UserInfoBean userInfoBean;
    //    private List<FindListBean.DataBean> data;
    private int category = -1;//刷新的时候判断状态
    private int crmuserid;

    public static FindTabListFragment getInstance(int type) {
        FindTabListFragment findTabListFragment = new FindTabListFragment();
        Bundle args = new Bundle();
        args.putInt("type", type);
        findTabListFragment.setArguments(args);
        return findTabListFragment;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_find_tab_list;
    }

    @Override
    public void initView(View view) {

        mSmartRefreshLayout = (SmartRefreshLayout) view.findViewById(R.id.mSmartRefreshLayout_find);
        mRecyclerView = (RecyclerView) view.findViewById(R.id.fragment_find_tab_rv);

        if (null != getArguments()) {
            category = getArguments().getInt("type");
        }
        mSmartRefreshLayout.setRefreshHeader(new ClassicsHeader(getContext()));
        mSmartRefreshLayout.setOnRefreshListener(refreshlayout -> {
            initData();
        });

        //为RecyclerView设置布局管理器
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        //为RecyclerView设置分割线(这个可以对DividerItemDecoration进行修改，自定义)
        mRecyclerView.addItemDecoration(new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL));
        //动画
        mRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mAdapter = new FindTabAdatper(R.layout.fragment_find_tab_item);
        mAdapter.bindToRecyclerView(mRecyclerView);

//        mAdapter.setEmptyView(R.layout.layout_takefood_list_empty);
        initListener();
        userInfoBean = CacheUserInfo.getInstance().getsUserInfo();
        crmuserid = userInfoBean.getUser().getCrm_user_id();
        initData();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        mAdapter.setOnItemChildClickListener((adapter, view, position) -> {
            if (CacheUserInfo.getInstance().getServiceNum() > 0) {
                downsale(crmuserid, mAdapter.getItem(position).getId());
            } else {
//                MainWebActivity.startMainWebActivity(getContext(), BaseApis.TAOCAN, "购买名片包");
                initDialog();
            }
        });
    }
    public void initDialog() {
        //去除dialog状态栏
        int uiOptions = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_IMMERSIVE
                | View.SYSTEM_UI_FLAG_FULLSCREEN;
        ContactAdminDialog demandDialog = new ContactAdminDialog(getContext());
        demandDialog.show();
        demandDialog.setText(CacheUserInfo.getInstance().getServiceNum());
        demandDialog.getWindow().getDecorView().setSystemUiVisibility(uiOptions);
    }
    //-1 全部需求   1 学历需求  2 职业需求
    public void initData() {
        //crmuserid 等于0 没权限 不请求
        RetrofitManger.getInstance().createService(UserApi.class)
                .getlist(crmuserid, category)
                .compose(RxSchedulers.applySchedulers())
                .subscribe(new ResponseObserve<FindListBean>() {

                    @Override
                    public void onSuccess(ResponseEntity<FindListBean> responseEntity) {
                        mAdapter.setNewData(responseEntity.getData().getData());
                    }

                    @Override
                    public void onFail(String errorMsg) {
                        if (errorMsg != null && !errorMsg.equals("")) {
                            ToastHelper.show(errorMsg);
                        } else {
                            ToastHelper.show("服务器繁忙，请稍后再试");
                        }
                    }

                    @Override
                    public void onSubscribeStart(@io.reactivex.annotations.NonNull Disposable disposable) {
                        addDisposable(disposable);
                    }

                    @Override
                    public void onComplete() {
                        super.onComplete();
                        closeLoadingDialog();
                        if (mSmartRefreshLayout != null) mSmartRefreshLayout.finishRefresh();
                    }
                });

    }

    /**
     * 下载名片
     *
     * @param crm_user_id
     * @param id
     */
    private void downsale(int crm_user_id, int id) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("crm_user_id", crm_user_id);
        map.put("id", id);
        RequestBody bodyByMap = RequestBodyUtil.getRequestBodyByMap(map);
        showLoadingDialog();
        RetrofitManger.getInstance().createService(UserApi.class)
                .downsale(bodyByMap)
                .compose(RxSchedulers.<ResponseEntity<DownCardBean>>applySchedulers())
                .subscribe(new ResponseObserve<DownCardBean>() {

                    @Override
                    public void onSuccess(ResponseEntity<DownCardBean> responseEntity) {
                        closeLoadingDialog();
                        initData();
                        ToastHelper.show("请在我的用户里查看");
                        EventBusUtils.post(EventBusKeys.REFRESH_FIND_LIST);
                    }

                    @Override
                    public void onFail(String errorMsg) {
                        closeLoadingDialog();
                        if (errorMsg != null && !errorMsg.equals("")) {
                            ToastHelper.show(errorMsg);
                        } else {
                            ToastHelper.show("服务器繁忙，请稍后再试");
                        }
                    }

                    @Override
                    public void onSubscribeStart(@NonNull Disposable disposable) {
                        addDisposable(disposable);
                    }
                });
    }

    private void initListener() {


    }


}
